<?php
/*
*流量统计类
*/
class Services_Stats
{
	public $pageId;
	public $shopId;
	public $sellId;
	public $nick;
	public $mmdId;
	public $ip;
	public $pdo;
	public $visitTime;
	private $table = 'stats';
	
	/*
	访问记录插入
	*/
	public function visitorInsert()
	{
		$data = array();
		$data['PAGE_ID'] = $this->pageId;
		$data['SHOP_ID'] = $this->shopId;
		$data['SELL_ID'] = $this->sellId;
		$data['NICK'] = $this->nick;
		$data['IP'] = $this->ip;
		$data['MMD_ID'] = $this->mmdId;
		$data['TIME'] = $this->visitTime;
		try{
			$res = $this->pdo->insert($this->table,$data);
		}catch(Exception $e){
			$appLog->warn($e->getMessage());
			return false;
		}
		if($res){
			return true;
		}else{
			return false;
		}
	}
	
	/*
	PV查询
	*/
	public function selectPv()
	{
		$startTime = $this->visitTime.' 00:00:00';
		$endTime = $this->visitTime.' 23:59:59';
		$sql = "SELECT COUNT(*) FROM `$this->table` where SHOP_ID='$this->shopId' AND PAGE_ID='$this->pageId' AND TIME BETWEEN '$startTime' AND '$endTime'";
		try{
			$data = $this->pdo->get_all($sql);
		}catch(Exception $e){
			$appLog->warn($e->getMessage());
			return false;
		}
		return (int)$data[0]['COUNT(*)'];
	}
	
	/*
	UV查询
	*/
	public function selectUv()
	{
		$startTime = $this->visitTime.' 00:00:00';
		$endTime = $this->visitTime.' 23:59:59';
		$sql = "SELECT COUNT(DISTINCT MMD_ID) FROM `$this->table` where SHOP_ID='$this->shopId' AND PAGE_ID='$this->pageId' AND TIME BETWEEN '$startTime' AND '$endTime'";
		try{
			$data = $this->pdo->get_all($sql);
		}catch(Exception $e){
			$appLog->warn($e->getMessage());
			return false;
		}
		return (int)$data[0]['COUNT(DISTINCT MMD_ID)'];
	}
	
	
	public function setPageId($pageId){
		if($pageId){
			$this->pageId = $pageId;
		}
	}
	
	public function setShopId($shopId){
		if($shopId){
			$this->shopId = $shopId;
		}
	}
	
	public function setSellId($sellId){
		if($sellId){
			$this->sellId = $sellId;
		}
	}
	
	public function setNick($Nick){
		if($Nick){
			$this->nick = $Nick;
		}
	}
	
	public function setMmdId($mmdId){
		if($mmdId){
			$this->mmdId = $mmdId;
		}
	}
	
	public function setIp($ip){
		if($ip){
			$this->ip = $ip;
		}
	}
	
	public function setPdo($pdo){
		if($pdo){
			$this->pdo = $pdo;
		}
	}
	
	public function setVisitTime($visitTime){
		if($visitTime){
			$this->visitTime = $visitTime;
		}
	}
	
}
?>